<?php
/*
 * @Author: 李明
 * @Date: 2021-11-20 18:15:20
 * @LastEditTime: 2022-02-20 12:23:20
 * @LastEditors: 李明
 * @Description:
 * @FilePath: /CRM2.0-PHP/app/Http/Controllers/app/order/UserOrderController.php
 * @山东壹零贰肆信息科技有限公司
 */

namespace App\Http\Controllers\app\order;

use App\Http\Controllers\common\LMReturn;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Utils;
use App\Http\Resources\Order\AppOrder;
use App\Http\Resources\Order\InterestOrder;
use App\Http\Resources\Order\OrderDetail;
use App\Http\Resources\OrderInterest\OrderInterestRecord;
use App\Http\Resources\OrderV2;
use App\Models\InterestRecord;
use App\Models\Order;
use App\Models\User;
use Illuminate\Http\Request;

class UserOrderController extends Controller
{
    /**
     * @description: 查询订单记录
     * @param {Request} $request
     * @return {*}
     */
    public function queryUserOrder(Request $request)
    {
        // 处理数据
        try {
            //去验证token值
            $tokenInfo = json_decode($this->verifyTokenLm('user'), true);
            //判断传递过来的token的code码是否等于400  如果是400的话，提示token不正确
            if ($tokenInfo['code'] == 400) {
                return LMReturn::tokenErr($tokenInfo['msg'], $tokenInfo['code']);
            }
            $all = $request->all();
            $status = $all["status"] ?? '';
            $user_id = $tokenInfo['user_id'];
            $user = User::find($user_id);
            $id_num = $user->id_number ?? '';
            if ($id_num != '') {
                $user_list = User::where('id_number', $id_num)->get();
                $user_id_list = [];
                foreach ($user_list as $user_c) {
                    $user_id_list[] = $user_c->id;
                }
            } else {
                $user_id_list = [$user->id];
            }
            // dd($user_id_list);
        } catch (\Throwable $th) {
            return LMReturn::Error('处理参数时发生错误', 'UOCQU01');
        }
        // 查询数据
        try {
            if ($status == '1') {
                $whereIn_order_status_list = ['3', '4'];
            } elseif ($status == '2') {
                $whereIn_order_status_list = ['6', '8',];
            } else {
                $whereIn_order_status_list = ['3', '4', '6', '8',];
            }
            $order_list = Order::whereIn('user_id', $user_id_list)
                ->whereIn("order_status", $whereIn_order_status_list)
                ->get();
            // $order_list = $user->orders
            //     ->whereIn('user_id', $user_id_list)
            //     ->whereIn("order_status", $whereIn_order_status_list)
            //     ->all();
            $order_list_res = AppOrder::collection($order_list);
            return LMReturn::Success($order_list_res);
        } catch (\Throwable $th) {
            return LMReturn::Error('查询时发生错误', 'UOCQU02');
        }
    }
    /**
     * @description: 手机端使用的额收益记录
     * @param {Request} $request
     * @return {*}
     */
    public function queryUserSyRecord(Request $request)
    {
        // 处理数据
        try {
            //去验证token值
            $tokenInfo = json_decode($this->verifyTokenLm('user'), true);
            //判断传递过来的token的code码是否等于400  如果是400的话，提示token不正确
            if ($tokenInfo['code'] == 400) {
                return LMReturn::tokenErr($tokenInfo['msg'], $tokenInfo['code']);
            }
            $all = $request->all();
            $user_id = $tokenInfo['user_id'];
            $user = User::find($user_id);
            $id_num = $user->id_number ?? '';
            $type = $all['type'] ?? '2';
            if ($id_num != '') {
                $user_list = User::where('id_number', $id_num)->get();
                $user_id_list = [];
                foreach ($user_list as $user_c) {
                    $user_id_list[] = $user_c->id;
                }
            } else {
                $user_id_list = [$user->id];
            }
            $where = [];
            if (isset($all['order_id'])) {
                $where[] = ['order_id', $all['order_id']];
            }
        } catch (\Throwable $th) {
            return LMReturn::Error('处理参数时发生错误', 'UOCQU01');
        }
        // 查询数据   // 1待打款，2打款中，3已打款
        try {
            if ($type == '1') { //   1累计收益 2预期收益
                $status_list = [3];
            } else if ($type == '2') {
                $status_list = [2, 1];
            }
            $whereIn_order_status_list = ['3', '4', '6', '8',];
            $order_id_list = Order::select('id')
                ->whereIn('user_id', $user_id_list)
                ->whereIn("order_status", $whereIn_order_status_list)
                ->get();
            $i_list = InterestRecord::where($where)
                ->whereIn('order_id', $order_id_list)
                ->whereIn('status', $status_list)
                ->get();
            $interest_list = InterestOrder::collection($i_list);
            return LMReturn::Success($interest_list);
        } catch (\Throwable $th) {
            return LMReturn::Error('查询时发生错误', 'UOCQU02');
        }
    }
}
